package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commen.Result;
import com.itheima.entiry.Category;
import com.itheima.entiry.Dish;
import com.itheima.entiry.DishDto;
import com.itheima.entiry.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(value = "dishCache",allEntries=true)
    public Result add(@RequestBody DishDto dishDto){
        dishService.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (flavors!=null){
            Long id = dishDto.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setId(id);
                dishFlavorService.save(flavor);
            }
        }
        return new Result(1,"添加成功");
    }

    @GetMapping("/page")
    public Result getPages(Integer page,Integer pageSize,String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(name!=null,Dish::getName,name);
        dishService.page(dishPage,qw);

        long total = dishPage.getTotal();
        List<Dish> records = dishPage.getRecords();
        System.err.println(records);
        Map map = new HashMap<>();
        map.put("total",total);
//        List<DishDto> collect = records.stream().map(new Function<Dish, DishDto>() {
//            @Override
//            public DishDto apply(Dish dish) {
//                DishDto dishDto = new DishDto();
//                BeanUtils.copyProperties(dish, dishDto);
//                Long categoryId = dishDto.getCategoryId();
//                Category byId = categoryService.getById(categoryId);
//                dishDto.setCategoryName(byId.getName());
//                return dishDto;
//            }
//        }).collect(Collectors.toList());
        List<DishDto> dishDtos=new ArrayList<>();
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            Long categoryId = record.getCategoryId();
            System.err.println(categoryId);
            Category byId = categoryService.getById(categoryId);
            System.out.println("byId = " + byId);
            if (byId!=null){
                String categoryName = byId.getName();
                BeanUtils.copyProperties(record,dishDto);
                dishDto.setCategoryName(categoryName);
            }
            dishDtos.add(dishDto);
        }
        map.put("records",dishDtos);

        return new Result(1,map);

    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Dish byId = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(byId,dishDto);
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,byId.getId());
        List<DishFlavor> list = dishFlavorService.list(qw);
        dishDto.setFlavors(list);
        return new Result(1,dishDto);
    }
    @Cacheable(value = "dishCache" , key = "'dish_'+#p0")
    @GetMapping("/list")
    public Result getById2(Long categoryId){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        List<Dish> lists = dishService.list(qw);
        List<DishDto> dishDtoList =new ArrayList<>();
        for (Dish list : lists) {
            Long dishId = list.getId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            LambdaQueryWrapper<DishFlavor> dishFlavorqw = new LambdaQueryWrapper<>();
            dishFlavorqw.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorqw);
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(list,dishDto);
            dishDto.setFlavors(dishFlavorList);
            dishDto.setCategoryName(categoryName);
            dishDtoList.add(dishDto);
        }
        return new Result(1,dishDtoList);
    }


    @PutMapping
    @CacheEvict(value = "dishCache",allEntries=true)
    public Result update(@RequestBody DishDto dishDto){
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getId,flavor.getId());
            dishFlavorService.update(flavor,qw);
        }
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getId,dishDto.getId());
        dishService.update(dishDto,qw);
        return new Result(1,"修改成功");
    }

    @DeleteMapping
    @CacheEvict(value = "dishCache",allEntries=true)
        public Result delete(@RequestParam List<Long> ids){
        dishFlavorService.removeByIds(ids);
        dishService.removeByIds(ids);
        return new Result(1);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "dishCache",allEntries=true)
    public Result updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        Dish dish = new Dish();
        dish.setStatus(status);
        qw.in(Dish::getId,ids);
        dishService.update(dish,qw);
        return new Result(1);
    }


}
